package com.plsjavaweb.ui.controller;

import com.plsjavaweb.core.EmployeeSeatCore;
import com.plsjavaweb.core.SearchCore;
import com.plsjavaweb.pojo.EmployeeSeatPOJO;
import com.plsjavaweb.pojo.SearchPOJO;
import com.plsjavaweb.logging.LoggingConstants;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;

/**
 * Servlet implementation class Login
 */
@WebServlet("/Search")
public class Search extends HttpServlet {
	static Logger log = Logger.getLogger(Search.class);
	
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public Search() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	@SuppressWarnings("null")
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		log.info(LoggingConstants.entryMessage);

		HttpSession session = request.getSession(false);
		
		if (session == null ) {
			
			request.getRequestDispatcher("index.jsp")
			.forward(request, response);
			
	    } else if (session.getAttribute("user") == null) {

	    	request.getRequestDispatcher("index.jsp")
			.forward(request, response);
	    
	    } else if ((session.getAttribute("username") != null)) {
			
			request.setAttribute("searchList",null);	
			request.setAttribute("employeeSeats", null);
					
			RequestDispatcher rd = request.getRequestDispatcher("WEB-INF/search.jsp");
			rd.forward(request, response);

	    }
		
		log.info(LoggingConstants.exitMessage);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	@SuppressWarnings("null")
	protected void doPost(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		
		log.info(LoggingConstants.entryMessage);
		
		HttpSession session = request.getSession(false);
		
		if (session == null){
			
			request.getRequestDispatcher("index.jsp")
			.forward(request, response);
			
	    } else if(session.getAttribute("user") == null){

	    	request.getRequestDispatcher("index.jsp")
			.forward(request, response);
	    
	    } else if ((session.getAttribute("user") != null)) {

	    	String inputSearch = request.getParameter("search");
			
	    	if ((!inputSearch.isEmpty()) || (!inputSearch.equals(null)) ){
	    	
			List<SearchPOJO> searchList = new ArrayList<>();
			
			SearchCore searchCore = new SearchCore();
			searchList = searchCore.getSearchEmployee(inputSearch);
			
			request.setAttribute("searchParam", inputSearch);
			request.setAttribute("searchList",searchList);
			
			EmployeeSeatCore empSeatCore = new EmployeeSeatCore();
			
			List<EmployeeSeatPOJO> empSeats = empSeatCore.getEmployeeSeatsByBldgByFloor("PIC", "3F");
			request.setAttribute("employeeSeats", empSeats);
	    	
	    	} else {
	    		
	    	}
	    	
			request.getRequestDispatcher("WEB-INF/search.jsp")
			.forward(request, response);

	    }
		
	}

}
